//
//  AnimationAppStoreStylePush.m
//  ZPTransition
//
//  Created by mac on 2019/5/21.
//  Copyright © 2019 zhihuiketang. All rights reserved.
//

//设置动画时间
#define TransitionTimePush  0.8
#define TransitionTimePop  0.8

#import "AnimationAppStoreStylePush.h"
#import "UIViewController+ZPTransition.h"

#pragma mark Push
@implementation AnimationAppStoreStylePush

- (UIImage *)_imageFromView:(UIView *)snapView{
    UIGraphicsBeginImageContextWithOptions(snapView.frame.size, NO, [UIScreen mainScreen].scale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [snapView.layer renderInContext:context];
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

#pragma mark UIViewControllerAnimatedTransitioning
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
    return TransitionTimePush;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
    
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    
    UIView *toView = toVC.view;
    toView.alpha = 0;
    
    toView.userInteractionEnabled = NO;
    [[transitionContext containerView] addSubview:toView];
    
    NSArray *fromSubViews = [fromVC zp_transitionUIViewFrameViews];
    NSArray *toSubViews = [toVC zp_transitionUIViewFrameViews];
    
    //将fromVC需要动画的Views拷贝并加载到[transitionContext containerView]上
    NSMutableArray *fromSubViewCopyArr = [[NSMutableArray alloc]init];
    
    for (UIView *fromSubView in fromSubViews) {
        
        NSMutableArray *subViewArr = [NSMutableArray new];
        
        for (UIView *subView in fromSubView.subviews) {
            [subViewArr addObject:subView];
            subView.hidden = !subView.hidden;
        }
        //将fromVC的Views的cornerRadius归为0
        CGFloat corner = fromSubView.layer.cornerRadius;
        fromSubView.layer.cornerRadius = 0;
        UIImage *image = [self _imageFromView:fromSubView];
        fromSubView.layer.cornerRadius = corner;
        
        CGRect rect = [fromSubView convertRect:fromSubView.bounds toView:KeyWindow];
        UIImageView *fromSubViewCopy = [[UIImageView alloc]initWithImage:image];
        fromSubViewCopy.layer.cornerRadius = corner;
        fromSubViewCopy.frame = rect;
        [[transitionContext containerView] addSubview:fromSubViewCopy];
        [fromSubViewCopyArr addObject:fromSubViewCopy];
        
        for (UIView *subView in subViewArr) {
            subView.hidden = !subView.hidden;
        }
    }
    
    
    UIImageView *fromSubViewCopy0 = fromSubViewCopyArr[0];
    fromSubViewCopy0.image = [UIImage imageNamed:[fromVC zp_transitionUIViewImage]];
    fromSubViewCopy0.contentMode = UIViewContentModeCenter;
    fromSubViewCopy0.layer.masksToBounds = YES;
    
    UIView *lastView = [toSubViews lastObject];
    UIImage *lastImage = [self _imageFromView:lastView];
    CGRect newRect = [lastView convertRect:lastView.bounds toView:KeyWindow];
    CGFloat height = newRect.size.height;
    UIImageView *lastViewCopy = [[UIImageView alloc]initWithImage:lastImage];
    lastViewCopy.frame = CGRectMake(newRect.origin.x, CGRectGetMaxY(fromSubViewCopy0.frame), newRect.size.width, 0);
    [[transitionContext containerView] addSubview:lastViewCopy];
    
    [UIView animateWithDuration:TransitionTimePush delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:0.2 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        
        for (int i =0; i <fromSubViewCopyArr.count; i ++) {
            
            UIView *fromSubViewCopy = fromSubViewCopyArr[i];
            UIView *toSubView = toSubViews[i];
            CGRect rect = [toSubView convertRect:toSubView.bounds toView:KeyWindow];
            fromSubViewCopy.frame = rect;
            fromSubViewCopy.layer.cornerRadius = 0;
        }
        
        UIView *fromSubViewCopy0 = fromSubViewCopyArr[0];
        lastViewCopy.frame = CGRectMake(lastViewCopy.frame.origin.x, CGRectGetMaxY(fromSubViewCopy0.frame), lastViewCopy.frame.size.width, height);
        
    } completion:^(BOOL finished) {
        
        toView.alpha = 1;
        
        toView.userInteractionEnabled = YES;
        for (UIView *fromSubViewCopy in fromSubViewCopyArr) {
            [fromSubViewCopy removeFromSuperview];
        }
        [lastViewCopy removeFromSuperview];
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
    }];
}

@end



//////////////

#pragma mark Pop
@implementation AnimationAppStoreStylePop

- (UIImage *)_imageFromView:(UIView *)snapView{
    UIGraphicsBeginImageContextWithOptions(snapView.frame.size, NO, [UIScreen mainScreen].scale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [snapView.layer renderInContext:context];
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

#pragma mark UIViewControllerAnimatedTransitioning
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
    if (self.isInteravtion) {
        return TransitionTimePop + 0.2;
    }else{
        return TransitionTimePop;
    }
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
    if (self.isInteravtion) {
        [self animateTransitionInteraction:transitionContext];
    }else{
        [self animateTransitionNoInteraction:transitionContext];
    }
}

- (void)animateTransitionInteraction:(id<UIViewControllerContextTransitioning>)transitionContext{
 
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    toVC.view.userInteractionEnabled = NO;
    [[transitionContext containerView] addSubview:toVC.view];
    
    NSArray *fromSubViews = [fromVC zp_transitionUIViewFrameViews];
    NSArray *toSubViews = [toVC zp_transitionUIViewFrameViews];
    
    //毛玻璃背景
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    UIVisualEffectView *effectView =[[UIVisualEffectView alloc]initWithEffect:blurEffect];
    effectView.frame = KeyWindow.bounds;
    [[transitionContext containerView] addSubview:effectView];
    [[transitionContext containerView] addSubview:fromVC.view];

    //减少两个动画方法之间的延迟，将第二部分动画所需的方法提前实现
    fromVC.view.layer.cornerRadius = 8;
    fromVC.view.transform = CGAffineTransformMakeScale(0.8, 0.8);
    NSMutableArray *fromSubViewCopyArr = [[NSMutableArray alloc]init];
    
    //因为fromVC的views比toVC多一个,所以减1
    for (int i =0; i <fromSubViews.count -1; i ++) {
        
        NSMutableArray *subViewArr = [NSMutableArray new];
        
        UIView *fromSubView = fromSubViews[i];
        for (UIView *subView in fromSubView.subviews) {
            
            [subViewArr addObject:subView];
            subView.hidden = !subView.hidden;
        }
        
        UIImage *image = [self _imageFromView:fromSubView];
        CGRect rect = [fromSubView convertRect:fromSubView.bounds toView:KeyWindow];
        UIImageView *fromSubViewCopy = [[UIImageView alloc]initWithImage:image];
        fromSubViewCopy.layer.masksToBounds = YES;
        fromSubViewCopy.frame = rect;
        //        [[transitionContext containerView] addSubview:fromSubViewCopy];
        [fromSubViewCopyArr addObject:fromSubViewCopy];
        
        for (UIView *subView in subViewArr) {
            subView.hidden = !subView.hidden;
        }
    }
    
    for (UIView *toSubView in toSubViews) {
        toSubView.hidden = YES;
    }
    //将fromVC最后一个View放入fromVC第一个View的下面
    UIView *lastView = [fromSubViews lastObject];
    UIImage *image = [self _imageFromView:lastView];
    CGRect newRect = [lastView convertRect:lastView.bounds toView:KeyWindow];
    UIImageView *lastViewCopy = [[UIImageView alloc]initWithImage:image];
    lastViewCopy.frame = newRect;
    //    [[transitionContext containerView] addSubview:lastViewCopy];
    
    UIImageView *fromSubViewCopy0 = fromSubViewCopyArr[0];
    fromSubViewCopy0.image = [UIImage imageNamed:[fromVC zp_transitionUIViewImage]];
    fromSubViewCopy0.contentMode = UIViewContentModeCenter;
    fromSubViewCopy0.layer.masksToBounds = YES;
    
    fromVC.view.layer.cornerRadius = 0;
    fromVC.view.transform = CGAffineTransformIdentity;

    //动画
    fromVC.view.clipsToBounds = YES;//单独设置cornerRadius，不然他的子视图不会改变cornerRadius
    [UIView animateWithDuration:0.2 animations:^{
        
        fromVC.view.layer.cornerRadius = 8;
        fromVC.view.transform = CGAffineTransformMakeScale(0.8, 0.8);
        
    } completion:^(BOOL finished) {
        
        if (!transitionContext.transitionWasCancelled) {
            
            for (UIView *subView in fromSubViewCopyArr) {
                [[transitionContext containerView] addSubview:subView];
            }
            [[transitionContext containerView] addSubview:lastViewCopy];
            
            
            [UIView animateWithDuration:TransitionTimePop delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:0.2 options:UIViewAnimationOptionCurveEaseOut animations:^{
                
                for (int i =0; i <fromSubViewCopyArr.count; i ++) {
                    
                    UIView *fromSubViewCopy = fromSubViewCopyArr[i];
                    UIView *toSubView = toSubViews[i];
                    CGRect rect = [toSubView convertRect:toSubView.bounds toView:KeyWindow];
                    fromSubViewCopy.frame = rect;
                    fromSubViewCopy.layer.cornerRadius = toSubView.layer.cornerRadius;
                }
                
                //                UIImageView *fromSubViewCopy0 = fromSubViewCopyArr[0];
                lastViewCopy.frame = CGRectMake(fromSubViewCopy0.frame.origin.x, CGRectGetMaxY(fromSubViewCopy0.frame), fromSubViewCopy0.frame.size.width, 0);
                lastViewCopy.alpha = 0;
                
            } completion:^(BOOL finished) {
                
                for (UIView *fromSubViewCopy in fromSubViewCopyArr) {
                    [fromSubViewCopy removeFromSuperview];
                }
                for (UIView *toSubView in toSubViews) {
                    toSubView.hidden = NO;
                }
                [lastViewCopy removeFromSuperview];
                toVC.view.userInteractionEnabled = YES;
                //                [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
            }];
        }
        [effectView removeFromSuperview];
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
    }];
}

- (void)animateTransitionNoInteraction:(id<UIViewControllerContextTransitioning>)transitionContext{
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    toVC.view.userInteractionEnabled = NO;
    
    [[transitionContext containerView] addSubview:toVC.view];
    //    [[transitionContext containerView] addSubview:fromVC.view];
    
    NSArray *fromSubViews = [fromVC zp_transitionUIViewFrameViews];
    NSArray *toSubViews = [toVC zp_transitionUIViewFrameViews];
    
    
    NSMutableArray *fromSubViewCopyArr = [[NSMutableArray alloc]init];
    //因为fromVC的views比toVC多一个,所以减1
    for (int i =0; i <fromSubViews.count -1; i ++) {
        
        NSMutableArray *subViewArr = [NSMutableArray new];
        
        UIView *fromSubView = fromSubViews[i];
        for (UIView *subView in fromSubView.subviews) {
            
            [subViewArr addObject:subView];
            subView.hidden = !subView.hidden;
        }
        UIImage *image = [self _imageFromView:fromSubView];
        CGRect rect = [fromSubView convertRect:fromSubView.bounds toView:KeyWindow];
        UIImageView *fromSubViewCopy = [[UIImageView alloc]initWithImage:image];
        fromSubViewCopy.layer.masksToBounds = YES;
        fromSubViewCopy.frame = rect;
        [[transitionContext containerView] addSubview:fromSubViewCopy];
        [fromSubViewCopyArr addObject:fromSubViewCopy];
        
        for (UIView *subView in subViewArr) {
            subView.hidden = !subView.hidden;
        }
    }
    
    for (UIView *toSubView in toSubViews) {
        toSubView.hidden = YES;
    }
    //将fromVC最后一个View放入fromVC第一个View的下面
    UIView *lastView = [fromSubViews lastObject];
    UIImage *image = [self _imageFromView:lastView];
    CGRect newRect = [lastView convertRect:lastView.bounds toView:KeyWindow];
    UIImageView *lastViewCopy = [[UIImageView alloc]initWithImage:image];
    lastViewCopy.frame = newRect;
    [[transitionContext containerView] addSubview:lastViewCopy];
    
    UIImageView *fromSubViewCopy0 = fromSubViewCopyArr[0];
    fromSubViewCopy0.image = [UIImage imageNamed:[fromVC zp_transitionUIViewImage]];
    fromSubViewCopy0.contentMode = UIViewContentModeCenter;
    fromSubViewCopy0.layer.masksToBounds = YES;
    [fromVC.view removeFromSuperview];
    
    [UIView animateWithDuration:TransitionTimePop delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:0.3 options:UIViewAnimationOptionCurveEaseOut animations:^{
        for (int i =0; i <fromSubViewCopyArr.count; i ++) {
            UIView *fromSubViewCopy = fromSubViewCopyArr[i];
            UIView *toSubView = toSubViews[i];
            CGRect rect = [toSubView convertRect:toSubView.bounds toView:KeyWindow];
            fromSubViewCopy.frame = rect;
            fromSubViewCopy.layer.cornerRadius = toSubView.layer.cornerRadius;
        }
        lastViewCopy.frame = CGRectMake(fromSubViewCopy0.frame.origin.x, CGRectGetMaxY(fromSubViewCopy0.frame), fromSubViewCopy0.frame.size.width, 0);
        lastViewCopy.alpha = 0;
    } completion:^(BOOL finished) {
        if (!transitionContext.transitionWasCancelled) {
            for (UIView *toSubView in toSubViews) {
                toSubView.hidden = NO;
            }
        }
        for (UIView *fromSubViewCopy in fromSubViewCopyArr) {
            [fromSubViewCopy removeFromSuperview];
        }
        [lastViewCopy removeFromSuperview];
        toVC.view.userInteractionEnabled = YES;
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
    }];
}

@end
